package core.models;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;

public class DhcpClient {
	private String name;
	HashMap<String, String> options;
	
	private static final String OPTION_REGEX = "^\\s*((option\\s+)?[\\w\\-]+)\\s+(.+);\\s*";
	private static final String END_REGEX = "^\\s*}\\s*";

	
	public DhcpClient(String name) {
		this.name = name;
		options = new HashMap<String, String>();
	}

	public void processConfig(Scanner scanner) {
		while (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			if (line.matches(OPTION_REGEX)) {
				String[] option = line.trim().split(" ");
				options.put(option[0], StringUtils.chop(StringUtils.join(option, " ", 1, option.length)));
			} else if (line.matches(END_REGEX)) {
				break;
			}
		}
	}

	@Override
	public String toString() {
		return name;
	}
	
	public String toConfigFormat() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("   host");
		buffer.append(" ");
		buffer.append(this.name);
		buffer.append(" {\n");
		for (Map.Entry<String, String> entry : options.entrySet()) {
			buffer.append("      ");
			buffer.append(entry.getKey());
			buffer.append(" ");
			buffer.append(entry.getValue());
			buffer.append(";\n");
		}
		buffer.append("   }\n\n");
		return buffer.toString();
	}

	public HashMap<String, String> getOptions() {
		return options;
	}

	public void setOptions(HashMap<String, String> options) {
		this.options = options;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
